﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Microsoft.WindowsMobile.Status;
using System.Runtime.InteropServices;

namespace CallBlock
{
    public partial class frmMain : Form
    {
        public frmMain()
        {
            InitializeComponent();
        }

        private string RemoveText(string number)
        {
            number = number.Replace("(", string.Empty);
            number = number.Replace(")", string.Empty);
            number = number.Replace("-", string.Empty);
            number = number.Replace(" ", string.Empty);

            return number;
        }

        const int KEYEVENTF_KEYDOWN = 0x0;
        const int KEYEVENTF_KEYUP = 0x2;

        [DllImport("coredll.dll", EntryPoint = "keybd_event", SetLastError = true)]
        internal static extern void keybd_event(byte bVk, byte bScan, int dwFlags, int dwExtraInfo);

        SystemState phoneIncomingCall = new SystemState(SystemProperty.PhoneIncomingCall);
        private void frmMain_Load(object sender, EventArgs e)
        {
            phoneIncomingCall.Changed += new ChangeEventHandler(phoneIncomingCall_Changed);
        }

        void phoneIncomingCall_Changed(object sender, ChangeEventArgs args)
        {
            string phoneNumber = RemoveText(SystemState.PhoneIncomingCallerNumber);

            if(listBoxBlockedNumbers.Items.Contains(phoneNumber))
            {
                keybd_event(115, 0, KEYEVENTF_KEYDOWN, 0);
                keybd_event(115, 0, KEYEVENTF_KEYUP, 0);
            }
        }

        private void menuItemBlock_Click(object sender, EventArgs e)
        {
            if (textBoxPhoneNumber.Text.Length == 0)
            {
                MessageBox.Show("Phone number cannot be empty.", "Alert!", MessageBoxButtons.OK, MessageBoxIcon.Asterisk, MessageBoxDefaultButton.Button1);
                return;
            }

            if (textBoxPhoneNumber.Text.Length < 7)
            {
                MessageBox.Show("Invalid phone number.", "Alert!", MessageBoxButtons.OK, MessageBoxIcon.Asterisk, MessageBoxDefaultButton.Button1);
                return;
            }

            listBoxBlockedNumbers.Items.Add(RemoveText(textBoxPhoneNumber.Text.Trim()));
            textBoxPhoneNumber.Text = string.Empty;
        }

        private void menuItemUnblock_Click(object sender, EventArgs e)
        {
            if (listBoxBlockedNumbers.SelectedIndex == -1)
            {
                MessageBox.Show("Nothing selected.", "Alert!", MessageBoxButtons.OK, MessageBoxIcon.Asterisk, MessageBoxDefaultButton.Button1);
                return;
            }

            listBoxBlockedNumbers.Items.Remove(listBoxBlockedNumbers.SelectedItem);
        }

        private void menuItemUnblockAll_Click(object sender, EventArgs e)
        {
            listBoxBlockedNumbers.Items.Clear();
        }

        private void menuItemAbout_Click(object sender, EventArgs e)
        {
            frmAbout aboutForm = new frmAbout();
            aboutForm.ShowDialog();
        }

        private void menuItemExit_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}